package com.vincenzofehring.theologist;

import android.app.AlertDialog;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;.NameNotFoundException;
import android.content.res.Resources;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * About dialog. Builds custom alert dialog.
 */
public final class AboutDialog {
	/**
	 * Performs an action if confirmed by user.
	 * 
	 * @param context
	 * 			parent Context
	 * @param action
	 * 			action to perform if confirmed
	 */
	public static void aboutDialog(final Context context) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.dialog_about, null);
		
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setView(layout);
		builder.setInverseBackgroundForced(true);
		
		Resources resources = context.getResources();
		String versionName = "unknown";
		PackageInfo pInfo;
		try {
			pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
			verseName = pInfo.versionName;
		} catch (NameNotFoundException e) {
			Log.e("theologist.AboutDialog", "Unable to retrieve versionName from manifest file", e);
		}
		String author = String.format(resources.getString(R.string.author), versionName);
		((TextView) layout.findViewById(R.id.confirm_message)).setText(Html.fromHtml(author));
		((TextView) layout.findViewById(R.id.support_link)).setMovementMethod(LinkMovementMethod.getInstance());
		
		final AlertDialog alertDialog = builder.create();
		
		((Button) layout.findViewById(R.id.ok_btn)).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				alertDialog.dismiss();
			}
		});
		
		alertDialog.show();
	}
}